package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.AlbumAddNewParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVo;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVo;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/albums")
@Api(tags = "04. 相册管理模块")/*@Api：添加在控制器类上，通过此注解的tags属性，可以配置模块名称*/
@Slf4j
@Validated
public class AlbumController {

    @Autowired
    private AlbumService albumService;



    /*
     * 在开发实践中，对请求方式的选取，比较通用的原则是：
     * 如果客户端提交的请求主要目的是为了获取数据，则使用GET，否则，使用POST
     * 限制方式有两种:
     * 一.@RequestMapping(value = "/add-new", method = {RequestMethod.GET,RequestMethod.POST})
     * 二.@RequestMapping的组合注解
     * */
    // http://localhost:9180/albums/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加相册")/*@ApiOperation：添加在处理请求的方法上，通过此注解的value属性，可以配置业务功能名称*/
    @ApiOperationSupport(order = 100)/*可以做排序用*/
    /*@Valid 是Validation中的注解验证参数是否合法 加在封装的参数上*/
    public JsonResult addNew(@Valid AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加相册】的请求，参数：{}", albumAddNewParam);
        albumService.addNew(albumAddNewParam);

        return JsonResult.ok();
    }

    // http://localhost:9180/albums/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    /*此注解跟@ApiModelProperty功能一样设置属性值，直接用在方法上*/
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    /*@Range和@NotNull类似  参数没封装注解就直接加在参数上，但是方法的类上要加@Validated*/
    public JsonResult delete(@Range(min = 1, message = "根据ID删除相册失败，请提交合法的ID值！")
                         @PathVariable Long id) {
        log.debug("开始处理【根据ID删除相册】的请求,参数: {}",id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/update
    @PostMapping("/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "相册id",required = true,dataType = "long")
    public JsonResult update(
            @Range(min = 1,message = "修改的相册id必须大于0")
            @RequestParam Long id, @Valid AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册详情】的业务，参数：{}", albumUpdateInfoParam);
        albumService.updateInfoById(id,albumUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询相册详情失败，请提交合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据ID查询相册详情】的请求,参数: {}",id);
        AlbumStandardVo result = albumService.getStandardById(id);
        return JsonResult.ok(result);
    }


    // http://localhost:9180/albums
    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1,message = "查询相册列表失败，请提供正确的页码值!") Integer page,
                           String queryType) {
        log.debug("开始处理【查询相册列表】的请求，页码:{}",page);
        if (page==null || page<1){
            page=1;
        }
        PageData<AlbumListItemVo> pageData;
        if ("all".equals(queryType)){
            pageData = albumService.list(page,Integer.MAX_VALUE);
        } else {
            pageData = albumService.list(page);
        }
        log.debug("查询结束!");
        return JsonResult.ok(pageData);
    }

}
